package com.zeng.sboot.redis.demo.config.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class RedisKeyGenerator implements KeyGenerator {

    private static Logger logger = LoggerFactory.getLogger(RedisKeyGenerator.class);

    @Override
    public Object generate(Object target, Method method, Object... params) {
        //格式化缓存key字符串
        StringBuilder sb = new StringBuilder();
        //追加类名
        sb.append(target.getClass().getName());
        //追加方法名
        sb.append(".");
        sb.append(method.getName());
        //遍历参数并且追加
        for(Object o : params){
            sb.append(o.toString());
        }
        System.out.println("调用Redis缓存Key: " + sb.toString());
        logger.info("调用Redis缓存Key为 {}",sb.toString());
        return sb.toString();
    }
}
